/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wzhq.commTencent;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.wzhq.pacsai.HisPacsInter;
import com.wzhq.pacsai.MainConfig;
import com.wzhq.pacsai.PacsAI;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author GG
 */
public class PostTencent {

    private static final Logger LOG = LoggerFactory.getLogger(PostTencent.class);
    public static final String URLDICOM = "https://med-biz-pre.wecity.qq.com/api/aiyixue/uploadDicom/v2/20000262";
    public static final String URLREPORT = "https://med-biz-pre.wecity.qq.com/api/aiyixue/dicomresult/v2/20000262";
    public static final String TOKEN = "d9366a01b4010f41c01a1f97fc35407e";
    public static final String PARTNERID = "20000262";
    public static final String GETRESULT = "dicomresult";
    public static final String POSTDICOM = "uploadDicom";
    public static final String STORESCPDIR = "d:\\AImagesDownloads";
    //每次上传时最大影像数目
    public static final int MAXIMAGECOUNTS = 20;
    //每次上传时最大影像文件大小(序列之和) 50M
    public static final int MAXIMAGESIZE = 52428800;
    //发生错误时最大重试次数
    public static final int MAXREOPER = 5;
    //从PACS服务器读影像错误标记  0正常  1发生错误
    public static int ERRDCMOPERFLAG = 0;
    //上传影像错误标记  0正常  1发生错误
    public static int ERRPOSTOPERFLAG = 0;

    public static void main(String[] args) {
        boolean isScpError = false;
        boolean isScuError = false;
        MainConfig DatabaseConn = new MainConfig();
        DatabaseConn.configPlugin();

        try {
            PacsAI getImageService = new PacsAI();
            isScpError = getImageService.startScpService();
        } catch (IOException ex) {
            isScpError = true;
            LOG.error("启动图像接收服务错误Exception:");
            LOG.error(ex.toString());
            java.util.logging.Logger.getLogger(PostTencent.class.getName()).log(Level.SEVERE, null, ex);
        }
        ScheduledExecutorService downLoadImgService = Executors.newScheduledThreadPool(1);
        ScheduledExecutorService postImgService = Executors.newScheduledThreadPool(1);
        ScheduledExecutorService receiveReportService = Executors.newScheduledThreadPool(1);
        ScheduledExecutorService syncHisRis = Executors.newScheduledThreadPool(1);

        long initialDelayHisPacsInter = 1;
        long initialDelayPacsAI = 3;
        long initialDelayPostDcm = 6;
        long initialDelayReceiveReport = 9;

        long period = 10;

        try {
            //a.postJsonDcm(URLDICOM, POSTDICOM);
            // a.getReport(URLREPORT, GETRESULT);

            // 固定调用时间长度，如执行时间大于调用时间长度，则执行完成后立即调用
            receiveReportService.scheduleAtFixedRate(new HisPacsInter("取RIS待上传记录服务"), initialDelayHisPacsInter, period, TimeUnit.SECONDS);
            downLoadImgService.scheduleAtFixedRate(new PacsAI("PACS图像下载服务"), initialDelayPacsAI, period, TimeUnit.SECONDS);
            postImgService.scheduleAtFixedRate(new PostDcm("上传图像服务"), initialDelayPostDcm, period, TimeUnit.SECONDS);
            receiveReportService.scheduleAtFixedRate(new ReceiveReport("取报告服务"), initialDelayReceiveReport, period, TimeUnit.SECONDS);

            //每隔2秒钟执行一次
            //service.scheduleWithFixedDelay(new MyScheduledExecutor("job2"), initialDelay, period, TimeUnit.SECONDS);
        } catch (Exception ex) {
            java.util.logging.Logger.getLogger(PostTencent.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
